package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.achievement.service.ReportService;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.entity.Report;
import com.lanou3g.ptp.entity.ReportSponsor;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.AlertBody;
import com.lanou3g.ptp.vo.ReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 创建人: 武奇
 * 创建事件: 2019/8/29
 * 阶段报告相关接口
 */
@Api(description = "阶段报告接口")
@RestController
public class ReportController {

    @Reference
    private ReportService reportService;

    @ApiOperation("学生查询需要提交的阶段报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务ID")
    })
    @GetMapping("/task/{taskId}/report/sponsors")
    public PtpResult findReportsToPost(@ApiIgnore @SsoStudent Student student,
                                       @PathVariable Long taskId) {
        List<ReportSponsor> reportSponsors = reportService.findReportsToPostByTaskId(student.getId(), taskId);
        return PtpResultUtils.ok(reportSponsors);
    }

    @ApiOperation("学生查询已提交的阶段报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务ID")
    })
    @GetMapping("/task/{taskId}/myReports")
    public PtpResult findMyReports(@ApiIgnore @SsoStudent Student student,
                                   @PathVariable Long taskId) {
        List<Report> reports = reportService.findMyReportsByTaskId(student.getId(), taskId);
        return PtpResultUtils.ok(reports);
    }

    @ApiOperation("通过ID查询阶段报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportId", value = "阶段报告ID")
    })
    @GetMapping("/report/{reportId}")
    public PtpResult findMyReports(@PathVariable Long reportId) {
        Report report = reportService.findById(reportId);
        return PtpResultUtils.ok(report);
    }

    @ApiOperation("学生提交阶段报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/report")
    public PtpResult postReport(@RequestBody Report report,
                                @ApiIgnore @SsoStudent Student student) {


        report.setStudentId(student.getId());

        boolean success = reportService.postReport(report);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(430, "阶段报告提交失败");
    }

    @ApiOperation("查询某个任务下某个学生的阶段报告")
    @GetMapping("/task/{taskId}/student/{studentId}/reports")
    public PtpResult reportsByTaskAndStu(@PathVariable Long taskId, @PathVariable Long studentId) {
        List<ReportVO> list = this.reportService.findByTaskAndStudent(taskId, studentId);
        return PtpResultUtils.ok(list);
    }

    @ApiOperation("提醒某个学生上传某个任务下的阶段报告")
    @PostMapping("/report/alert")
    public PtpResult alertDiary(@RequestBody AlertBody alert) {
        // TODO: 2019/8/29 阶段报告上传提醒
        boolean b = this.reportService.alertReport(alert);
        return b ? PtpResultUtils.ok() : PtpResultUtils.error(505, "发送失败");
    }

    @ApiOperation("修改阶段报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PutMapping("/report/{id}")
    public PtpResult update(
            @ApiIgnore @SsoStudent Student student, @RequestBody Report report, @PathVariable Long id
    ) {
        report.setId(id);

        System.out.println(report);
        boolean success = reportService.putReport(report);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(430, "提交修改阶段报告失败");
    }


    @ApiOperation("教师发起阶段性报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/task/{taskId}/report/sponsor")
    public PtpResult sponsorReport(@PathVariable Long taskId) {
        boolean b = reportService.insertReportSponsor(taskId);
        return b ? PtpResultUtils.ok() : PtpResultUtils.error(430, "提交修改阶段报告失败");
    }

}
